package village.rainbow.slipin;

import java.util.List;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.util.Log;

public class DataProvider extends ContentProvider{

	public static final String AUTHORITY   = "village.rainbow.slipin.contentprovider";
	public static final Uri    CONTENT_URI  =  Uri.parse("content://" + AUTHORITY);

	//private String Auth_key = "DAILYSCENEAUTHKEY";

	private String Auth_key = "village";
	
	private Context _context;
	
	public String getAuthkey() {
		return Auth_key;
	}

	public void setAuthkey(String authkey) {
		Auth_key = authkey;
	}

	@Override
	public String getType(Uri uri) {
		return null;
	}

	/**
	 * ContentProvider 객체가 생성 되면 호출
	 */
	@Override
	public boolean onCreate() {
		_context = getContext();
		return false;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {

		Log.d("PROVIDERT", "A_insert()");
		
//		List<String> reqValue = uri.getPathSegments();
//
//		if(reqValue.size() > 0) {
//			String serviceType = reqValue.get(0);
//			Log.d("PROVIDERT", "A_serviceType = " + serviceType);
//			
//			if(serviceType.equals("AUTH_GET")){
//
//				return Uri.parse(CONTENT_URI + "/" + serviceType + "/" + getAuthkey());
//			}
//		}
		
		int id = values.getAsInteger("SCENE_ID");
		Uri _uri = ContentUris.withAppendedId(CONTENT_URI, id);
		
		//_context.getContentResolver().notifyChange(_uri, null);
		
		return _uri;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {

		Log.i("TEST", uri.getQuery());

		return null;
	}
	
	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder,
			CancellationSignal cancellationSignal) {
		// TODO Auto-generated method stub
		return super.query(uri, projection, selection, selectionArgs, sortOrder,
				cancellationSignal);
	}
	
	@Override
	public Bundle call(String method, String arg, Bundle extras) {
		return super.call(method, arg, extras);
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {

		Log.d("PROVIDERT", "A_update()");

		List<String> reqValue = uri.getPathSegments();

		if(reqValue.size() > 0) {
			String serviceType = reqValue.get(0);
			Log.d("PROVIDERT", "A_serviceType = " + serviceType);

			if(serviceType.equals("AUTH_UPDATE")){

				String new_authkey = values.getAsString("new_authkey");
				Log.i("PROVIDERT", "update() new_authkey = " + new_authkey);

				setAuthkey(new_authkey);
			}
		}

		return 0;
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		return 0;
	}
	

}
